<?php
/**
 * This file is part of the Agora distribution. 
 * Detailed copyright and licensing information can be found
 * in the gpl-3.0.txt file which should be included in the distribution.
 * 
 * @version		$Id: 3.0.14 Olympus 2010-09-28 rammstein4o $
 * @copyright  2007 - 2010 jVitals
 * @license   GPLv3 Open Source
 * @link       http://jvitals.com
 * @since      File available since initial release
 */

class AUserHelper {

	function checkSize($img_size) {
		
		if (!is_array($img_size) || (!(int)$img_size[0] || !(int)$img_size[1])) {
			return '';
		}
		
		switch (intval($this->agora_config['o_avatars_ratio']) ) {
			case '3':  // exact
				$height 	= " height=\"". intval($this->agora_config['o_avatars_dheight']) ."\"";
				$width 	= " width=\"". intval($this->agora_config['o_avatars_dwidth']) ."\"";
				break;
			case '2': // both
				$width 	= intval($this->agora_config['o_avatars_dwidth']);
				$ratio 	= $width / $img_size[0];
				$height	= intval($img_size[1] * $ratio);

				if ($height > intval($this->agora_config['o_avatars_dheight'])) {
					$ratio	= intval($this->agora_config['o_avatars_dheight']) / $height;
					$height 	= intval($this->agora_config['o_avatars_dheight']);
					$width	= intval($width * $ratio);
				}
				$width 	= " width=\"". $width ."\"";
				$height 	= " height=\"". $height ."\"";
				break;

			case '1':  // height
				$height 	= intval($this->agora_config['o_avatars_dheight']);
				$ratio 	= $height / $img_size[1];
				$width 	= " width=\"". intval($img_size[0] * $ratio)."\"";
				$height 	= " height=\"". $height ."\"";
				break;

			case '0':
			default :
				$width 	= intval($this->agora_config['o_avatars_dwidth']);
				$ratio 	= $width / $img_size[0];
				$height 	= " height=\"". intval($img_size[1] * $ratio)."\"";
				$width 	= " width=\"". $width ."\"";
				break;
		}
		return $width.$height;
	}
	
	
	private function _getDefaultAvatar() {
		jimport('joomla.filesystem.file');
		if (JFile::exists(rtrim(JPATH_ROOT,'\\/').DS.trim($this->agora_config['o_avatars_dir'],'\\/').DS.'noavatar.jpg')) {
			return rtrim(JURI::root(),'/') . '/' . trim($this->agora_config['o_avatars_dir'],'/') . '/noavatar.jpg';
		}
		return Agora::getRoot().'img/pre_avatars/noavatar.jpg';
	}
	
	/*
	  prepares users Avatar for display
	  $show_if_profile - under Profile -> Personality show avatar even if Use Avatar has been unchecked 
	  (so a user is however able to see his own avatar in this section) 
	*/
	
	function prepareUserAvatar($user, $show_if_profile = 0) {
		
		// if Guest, no need to go further
		if (!(int)$user['id']) {
			$user['avatar'] = self::_getDefaultAvatar();
			$user['avatar_size'] = '';
			return $user;
		}
		
		$use_avatar = isset($user['use_avatar']) ? (int)$user['use_avatar'] : 0;
		
		if (intval($this->agora_config['o_joomsocialsuite'])) {

			$jss_model = &Model::getInstance('jssintegrationModel');

			$jSSavatar = $jss_model->getjSSAvatar($user['jos_id']);
			if ($jSSavatar) {
				
				$path = JPATH_BASE . DS . 'images' . DS . 'jsocialsuite' . DS . 'avatar'. DS  ;
				if (file_exists($path. $jSSavatar.'.gif')) {
					$avatarName 	= $jSSavatar.'.gif' ;
				} elseif (file_exists($path. $jSSavatar.'.jpg')) {
					$avatarName 	= $jSSavatar.'.jpg' ;
				} elseif (file_exists($path. $jSSavatar.'.png')) {
					$avatarName 	= $jSSavatar.'.png' ;
				} else {
					$avatarName 	= FALSE ;
				}
				if ($avatarName) {
					$avatarSz = $path . $avatarName;
					if ($img_size = @getimagesize($avatarSz))
						$user['avatar_size'] = $img_size[3];
						$jSSavatar = JURI::root(). 'images/jsocialsuite/avatar/'. $avatarName .'"'.$this->checkSize($img_size);
				} else {
					$jSSavatar =Agora::getRoot().'img/pre_avatars/noavatar.jpg';
				}
			} else {
				$jSSavatar =Agora::getRoot().'img/pre_avatars/noavatar.jpg';
			}

			$user['avatar'] = $jSSavatar;
		} else if (intval($this->agora_config['o_community_builder'])) {

			$cb_model = &Model::getInstance('CbModel');

			$cb_avatar = $cb_model->getCbAvatar($user['jos_id']);
			
			if ($cb_avatar) {

				$cb_avatarSz = JPATH_BASE . DS . 'images'. DS . 'comprofiler'. DS. $cb_avatar;
				$this->checkSize(@getimagesize($cb_avatarSz));
				if (file_exists($cb_avatarSz)) {
					if ($img_size = @getimagesize($cb_avatarSz)) {
						$user['avatar_size'] = $img_size[3];
					}
					
					$cb_avatar = JURI::root(). 'images/comprofiler/'. $cb_avatar .'"'.$this->checkSize($img_size);
				} else {
					$cb_avatar =Agora::getRoot().'img/pre_avatars/noavatar.jpg';
				}

			} else {
				$cb_avatar =Agora::getRoot().'img/pre_avatars/noavatar.jpg';
			}

			$user['avatar'] = $cb_avatar;

		} else if (intval($this->agora_config['o_joomsocialsuite'])) {

				$joomsocialsuite_avatar =Agora::getRoot().'img/pre_avatars/noavatar.jpg';


				$user['avatar'] = $joomsocialsuite_avatar;

		} else if (intval($this->agora_config['o_jomsocial'])) {

			$Jomsocial_model = &Model::getInstance('JomsocialModel');

			$Jomsocial_avatar = $Jomsocial_model->getJomsocialAvatar($user['jos_id']);
			if ($Jomsocial_avatar) {

				$Jomsocial_avatarSz = JPATH_BASE . DS . $Jomsocial_avatar;
				
				if (file_exists($Jomsocial_avatarSz)) {
					if ($img_size = @getimagesize($Jomsocial_avatarSz)) {
						$user['avatar_size'] = $img_size[3];
					}
					$Jomsocial_avatar = JURI::root(). $Jomsocial_avatar.'"'.$this->checkSize($img_size);
				} else {
					$Jomsocial_avatar =Agora::getRoot().'img/pre_avatars/noavatar.jpg';
				}
			} else {
				$Jomsocial_avatar =Agora::getRoot().'img/pre_avatars/noavatar.jpg';
			}

			$user['avatar'] = $Jomsocial_avatar;

		} else if (intval($this->agora_config['o_joomunity'])) {

			$joomunity_model = &Model::getInstance('JoomunityModel');
			$joomunity_avatar = $joomunity_model->getJoomunityAvatar($user['jos_id']);
			if ($joomunity_avatar) {
				//components/com_joomunity/files/avatars/
				$joomunity_avatarSz = JPATH_BASE . DS . 'components'. DS . 'com_joomunity'. DS. 'files'  . DS. 'avatars' . DS. $joomunity_avatar;
				
				if (file_exists($joomunity_avatarSz)) {
					if ($img_size = @getimagesize($joomunity_avatarSz)) {
						$user['avatar_size'] = $img_size[3];
					}
					$joomunity_avatar = JURI::root(). 'components/com_joomunity/files/avatars/'. $joomunity_avatar.'"'.$this->checkSize($img_size);
				} else {
					$joomunity_avatar =Agora::getRoot().'img/pre_avatars/noavatar.jpg';
				}
			} else {
				$joomunity_avatar =Agora::getRoot().'img/pre_avatars/noavatar.jpg';
			}

			$user['avatar'] = $joomunity_avatar;

		} else { // agora
			$a_path = JPATH_BASE . DS . $this->agora_config['o_avatars_dir'] .DS. $user['id'];
			
			if ($img_size = @getimagesize($a_path.'.gif')) {
				// $avatar = Agora::getRoot().'img/pre_avatars/'.$user['id'].'.gif';
				$avatar = JURI::root(). $this->agora_config['o_avatars_dir'] .'/'.$user['id'].'.gif';
			} else if ($img_size = @getimagesize($a_path.'.jpg')) {
				// $avatar = Agora::getRoot().'img/pre_avatars/'.$user['id'].'.jpg';
				$avatar = JURI::root(). $this->agora_config['o_avatars_dir'] .'/'.$user['id'].'.jpg';
			} else if ($img_size = @getimagesize($a_path.'.png')) {
				// $avatar = Agora::getRoot().'img/pre_avatars/'.$user['id'].'.png';
				$avatar = JURI::root(). $this->agora_config['o_avatars_dir'] .'/'.$user['id'].'.png';
			}
			
			if (isset($avatar) && ((int)$use_avatar || (int)$show_if_profile)) {
				$user['avatar'] = $avatar;
				$user['avatar_size'] = $img_size[3];
			} else {
				// $user['avatar'] = JURI::root(). $this->agora_config['o_avatars_dir'] .'/noavatar.jpg';
				$user['avatar'] = self::_getDefaultAvatar();
				$user['avatar_size'] = '';
			}
		}
		
		return $user;
	}

	function prepareUserTitle($user)  // prepars user Title and Ranks
	{
		static $ranks = null;
		$use_ranks = intval($this->agora_config['o_ranks']);

		if ($use_ranks) {
			if (is_null($ranks)) {
				$ranks_model = & Model::getInstance('RanksModel');
				$ranks = $ranks_model->loadAll();
			}
		} else {
			$ranks = array();
		}

		$user['rank_image'] = '';

		if (!isset($user['id'])) {
			$user['id'] = 0;
		}

		if ($user['id'] == 0) {
			$user['title'] = Agora::lang('Guest');
			return $user;
		}

		if ($user['title'] != '')
			$user_title = Agora::escape($user['title']);
		elseif (isset($user['banned']) && $user['banned'])
		// If the user is banned
			$user_title = Agora::lang('Banned');
		elseif (isset($user['is_guest']) && $user['is_guest'])
		// If the user is a guest
			$user_title = Agora::lang('Guest');
		else
			$user_title = '';

		$user['title'] = $user_title;
		// Are there any ranks?

		if ($use_ranks) {
			foreach ($ranks as $rank) {
			    $user__ = & Model::getInstance('UserModel');
			    $usr_grps = $user__->getGroups($user['id']);
			    $user_type = '';
			    foreach ($usr_grps as $grp)
			    {
			        if ($grp['role'] == 'moderator' || $grp['role'] == 'admin')
			        $user_type = $grp['role'];
			    }
			    
			    if ($rank["user_type"])
			    {
			        if($user_type == $rank["user_type"])
			        {
			            $user['rank'] = Agora::escape($rank['rank']);
       					if ($rank['image']) {
						$user['rank_image'] = $rank['image'];
					    }
			            
			        }
			    }
			    else
			    {
				    if ( (intval($user['num_posts']) >= intval($rank['min_posts'])) && !$user_type ) {
					    $user['rank'] = Agora::escape($rank['rank']);
					    if ($rank['image']) {
						    $user['rank_image'] = $rank['image'];
					    }
				    }			        
			    }
			}
			
			if (!isset($user['rank'])) {
				$user['rank'] = '';
			}

			if (!isset($user['rank_image'])) {
				$user['rank_image'] = '';
			}
		}

		return $user;
	}

	function prepareUserUrl($user)  // Check and prepare url
	{
		if ($user['url'] && !stristr($user['url'],'http://')) {
			$user['url'] = "http://" . $user['url'];
		}

		return $user;
	}


}

